#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# copyright (c) 2010 by The Codedrawer Team
#
# 成员
# xkhhdx(HD.Huo)	viking9345(C.Cai)	renxing225(X.Ren)
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 代码来自：http://code.google.com/p/codedrawer/
#
#--- 更新记录 ---
#成员		时间			简述
#xkhhdx		21:27 2010-8-27	修正了遇到空字串定义代码时的着色错误
#xkhhdx		16:47 2010-7-20	添加了 C++ 语言的支持
#
#--- 计划列表 ---
#提出者		时间			是否解决	解决者	计划简述		解决简述
#xkhhdx		1:47 2010-6-20	否			n/a		添加异常处理	n/a
#

from core import *

class cpp_code_render(code_render):
    """ 对 C++ 代码进行标记 """
    def prepare(self):
        """ 准备正则式、正则库等信息 """
        # 注释
        self.regex_dict["comment"] = (	r'''(/\*[\s\S]*?\*/)''',						# C 风格的块注释
                                      	r'''(//.*(?=\n))|(//.*$)''',					# C++ 风格的行注释
                                      	#r'''(#.*(?=\n))|(#.*$)''',						# Perl 风格的行注释
                                      	r"""((?:\'\')'(?:\.|(\\\')|[^\''\n]|)+''')""",	# python 三个单引号
                                      	r"""('(?:\.|(\\\')|[^\''\n]|)+')""")				# 单引号引起来的

        # 字符串
        self.regex_dict["quoted_string"] = (	r'''((?:\"\")"(?:\.|(\\\")|[^\""\n]|)+""")''',	# python 三个双引号
                                            	r'''("(?:\.|(\\\")|[^\""\n]|)+")''',			# 双引号引起来的
                                            	r'''(&quot;(?:\.|(\\&quot;)|[^\n]|)+&quot;)''')	# 双引号引起来的

        # 关键字
        keyword_list = ( "break","case","catch","class","const","__finally","__exception","__try","const_cast","continue","private","public","protected","__declspec","default","delete","deprecated","dllexport","dllimport","do","dynamic_cast","else","enum","explicit","extern","if","for","friend","goto","inline","mutable","naked","namespace","new","noinline","noreturn","nothrow","register","reinterpret_cast","return","selectany","sizeof","static","static_cast","struct","switch","template","this","thread","throw","true","false","try","typedef","typeid","typename","union","using","uuid","virtual","void","volatile","whcar_t","while")
        # C++ 数据类型
        data_type = ( "ATOM","BOOL","BOOLEAN","BYTE","CHAR","COLORREF","DWORD","DWORDLONG","DWORD_PTR","DWORD32","DWORD64","FLOAT","HACCEL","HALF_PTR","HANDLE","HBITMAP","HBRUSH","HCOLORSPACE","HCONV","HCONVLIST","HCURSOR","HDC","HDDEDATA","HDESK","HDROP","HDWP","HENHMETAFILE","HFILE","HFONT","HGDIOBJ","HGLOBAL","HHOOK","HICON","HINSTANCE","HKEY","HKL","HLOCAL","HMENU","HMETAFILE","HMODULE","HMONITOR","HPALETTE","HPEN","HRESULT","HRGN","HRSRC","HSZ","HWINSTA","HWND","INT","INT_PTR","INT32","INT64","LANGID","LCID","LCTYPE","LGRPID","LONG","LONGLONG","LONG_PTR","LONG32","LONG64","LPARAM","LPBOOL","LPBYTE","LPCOLORREF","LPCSTR","LPCTSTR","LPCVOID","LPCWSTR","LPDWORD","LPHANDLE","LPINT","LPLONG","LPSTR","LPTSTR","LPVOID","LPWORD","LPWSTR","LRESULT","PBOOL","PBOOLEAN","PBYTE","PCHAR","PCSTR","PCTSTR","PCWSTR","PDWORDLONG","PDWORD_PTR","PDWORD32","PDWORD64","PFLOAT","PHALF_PTR","PHANDLE","PHKEY","PINT","PINT_PTR","PINT32","PINT64","PLCID","PLONG","PLONGLONG","PLONG_PTR","PLONG32","PLONG64","POINTER_32","POINTER_64","PSHORT","PSIZE_T","PSSIZE_T","PSTR","PTBYTE","PTCHAR","PTSTR","PUCHAR","PUHALF_PTR","PUINT","PUINT_PTR","PUINT32","PUINT64","PULONG","PULONGLONG","PULONG_PTR","PULONG32","PULONG64","PUSHORT","PVOID","PWCHAR","PWORD","PWSTR","SC_HANDLE","SC_LOCK","SERVICE_STATUS_HANDLE","SHORT","SIZE_T","SSIZE_T","TBYTE","TCHAR","UCHAR","UHALF_PTR","UINT","UINT_PTR","UINT32","UINT64","ULONG","ULONGLONG","ULONG_PTR","ULONG32","ULONG64","USHORT","USN","VOID","WCHAR","WORD","WPARAM","WPARAM","WPARAM","char","bool","short","int","__int32","__int64","__int8","__int16","long","float","double","__wchar_t","clock_t","_complex","_dev_t","_diskfree_t","div_t","ldiv_t","_exception","_EXCEPTION_POINTERS","FILE","_finddata_t","_finddatai64_t","_wfinddata_t","_wfinddatai64_t","__finddata64_t","__wfinddata64_t","_FPIEEE_RECORD","fpos_t","_HEAPINFO","_HFILE","lconv","intptr_t","jmp_buf","mbstate_t","_off_t","_onexit_t","_PNH","ptrdiff_t","_purecall_handler","sig_atomic_t","size_t","_stat","__stat64","_stati64","terminate_function","time_t","__time64_t","_timeb","__timeb64","tm","uintptr_t","_utimbuf","va_list","wchar_t","wctrans_t","wctype_t","wint_t","signed" )

        self.regex_dict["keyword"] = r"(\b" + r"\b|\b".join(keyword_list) + r"\b|\b".join(data_type) + r"\b)"

        self.verifier_dict["spliter"] = re.compile( "|".join(self.regex_dict["comment"]) + r"|" + "|".join(self.regex_dict["quoted_string"]))
        self.verifier_dict["comment"] =  re.compile( "|".join(self.regex_dict["comment"]) )
        self.verifier_dict["quoted_string"] =  re.compile( "|".join(self.regex_dict["quoted_string"]) )
        self.verifier_dict["keyword"] = re.compile(self.regex_dict["keyword"])

